const { verifyToken } = require("../utils/tokenUtils")
/* 
用于token校验的中间件
*/
module.exports = async (req, res, next) => {
  try {
    const token = req.headers['token']
    const userId = await verifyToken(token)
    req.userId = userId //将用户id传递给后面的中间件 
    next()
  } catch (error) {
    // 失败了, 返回一个401的错误响应
    console.log(error)
    res.statusCode = 401
    res.send({
      code: RES_CODE.DATA_ERROR,
      message: error.message
    })
  }
}